package com.example.test.springbootdemo.configuration;

import javax.sql.DataSource;

import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

@Configuration
@EnableTransactionManagement
public class SessionFactoryConfiguration {
	
	
	@Bean
	public LocalSessionFactoryBean sessionFactory() {
		LocalSessionFactoryBean session = new LocalSessionFactoryBean();
		session.setConfigLocation(new ClassPathResource("hibernate.cfg.xml"));
		
//		session.setDataSource(buildDataSource());
//		session.setPackagesToScan(folder);
//		Properties hibernate = new Properties();
//		hibernate.put("hibernate.dialect",      dialect);
//		hibernate.put("hibernate.show_sql",     "false");
//		hibernate.put("hibernate.hbm2ddl.auto", "false");
//		session.setHibernateProperties(hibernate);
		System.out.println("sessionFactory方法 LocalSessionFactoryBean的值"+session);
		return session;
	}

	@Bean
	public HibernateTransactionManager transactionManager() {
		HibernateTransactionManager m = new HibernateTransactionManager();
		SessionFactory s=sessionFactory().getObject();
		System.out.println("transactionManager方法 LocalSessionFactoryBean的值"+s);
		m.setSessionFactory(s);
		return m;
	}
	
	private DataSource dataSource() {
		HikariConfig config = new HikariConfig();
		
        final HikariDataSource ds = new HikariDataSource();
        
//        ds.setMaximumPoolSize(100);
//        ds.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
//        ds.addDataSourceProperty("url", url);
//        ds.addDataSourceProperty("user", username);
//        ds.addDataSourceProperty("password", password);
//        ds.addDataSourceProperty("cachePrepStmts", true);
//        ds.addDataSourceProperty("prepStmtCacheSize", 250);
//        ds.addDataSourceProperty("prepStmtCacheSqlLimit", 2048);
//        ds.addDataSourceProperty("useServerPrepStmts", true);
        return ds;
    }
	
//	@Bean
//	public SessionFactory getSessionFactory() {
//		StandardServiceRegistry serviceRegistry=null;
//		SessionFactory sessionFactory=null;
//			try {
//				StandardServiceRegistryBuilder registryBuilder = new StandardServiceRegistryBuilder();
//
//				Configuration config = new Configuration();
//				URL url = SessionFactoryConfiguration.class.getClassLoader().getResource("hibernate.cfg.xml");
//				config.configure(url);
//				
//				registryBuilder.applySettings(config.getProperties());
//				serviceRegistry = registryBuilder.build();
////				MetadataSources sources = new MetadataSources(serviceRegistry).addAnnotatedClass(Dict.class);
////				Metadata metadata = sources.getMetadataBuilder().build();
////				sessionFactory = metadata.getSessionFactoryBuilder().build();
//				sessionFactory = config.buildSessionFactory(serviceRegistry);
//			} catch (Exception e) {
//				if (serviceRegistry != null) {
//					StandardServiceRegistryBuilder.destroy(serviceRegistry);
//				}
//				e.printStackTrace();
//			}
//		System.out.println("sessionFactory的值"+sessionFactory);
//		return sessionFactory;
//	}

	
}
